//
//  ManJianPlayController.m
//  Experss
//
//  Created by 嗯，大葱 on 17/9/21.
//  Copyright © 2017年 嗯，大葱. All rights reserved.
//

#import "ManJianPlayController.h"
#import "NoTimeBuyOneCell.h"
#import "NoTimeBuyTwoCell.h"
#import "NoTimeBuyDetailController.h"
#import "FenLeiDDetailViewController.h"
#import "NoTimeBuyModel.h"
#import "ShouYeSixCell.h"
#import "InStoreController.h"

#import "NearlyShopModel.h"
#import "NearlyShopModel2.h"

@interface ManJianPlayController ()<UITableViewDelegate, UITableViewDataSource>
{
    UINib *_cellNib;
    
        int _page;
    
}
@property (nonatomic, strong)UIButton *rightBtn;
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *datarray;
@property (nonatomic, strong)NSMutableArray *datarray33;

@end

@implementation ManJianPlayController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.translucent = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _page = 1;
    _datarray = [NSMutableArray array];
    _datarray33 = [NSMutableArray array];
    
    [self configureNaBar];
    [self configureViewController];
    [self getNoTimeBuyDataFromApi];//获取限时秒杀数据}
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
   

- (void)configureNaBar {
        self.navigationItem.title = _titleStr;
        
        //设置导航栏按钮的边距
        _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightBtn.frame= CGRectMake(0, 0, 60, 30);
        [_rightBtn setImage:[UIImage imageNamed:@"all_fanhui2"] forState:(UIControlStateNormal)];
        [_rightBtn addTarget:self action:@selector(handleBackAxtion) forControlEvents:UIControlEventTouchUpInside];
        
        UIBarButtonItem *btn_left = [[UIBarButtonItem alloc] initWithCustomView:_rightBtn];
        UIBarButtonItem *negativeSpacerleft = [[UIBarButtonItem alloc]   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace   target:nil action:nil];
        /**
         width为负数时，相当于btn向右移动width数值个像素，由于按钮本身和  边界间距为5pix，所以width设为-5时，间距正好调整为0；width为正数 时，正好相反，相当于往左移动width数值个像素
         */
        negativeSpacerleft.width = -20;
        self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacerleft, btn_left, nil];
        
    }

- (void)handleBackAxtion {
        [self.navigationController popViewControllerAnimated:YES];
   
}

    
    

- (void)configureViewController {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, kScreen_Height-64) style:(UITableViewStylePlain)];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle =kSpNone;
        [self.view addSubview:_tableView];
        [_tableView registerNib:[UINib nibWithNibName:@"NoTimeBuyOneCell" bundle:nil] forCellReuseIdentifier:@"NoTimeBuyOneCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"NoTimeBuyTwoCell" bundle:nil] forCellReuseIdentifier:@"NoTimeBuyTwoCell"];
    
    _cellNib = [UINib nibWithNibName:@"ShouYeSixCell" bundle:nil];
    
    
    
    MJRefreshGifHeader *headNew = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestRefresh)];
    NSMutableArray *refreshingImages2 = [NSMutableArray array];
    for (NSUInteger i =1; i<=12; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld",i]];
        [refreshingImages2 addObject:image];
    }
    
    NSMutableArray *idleImages2 = [NSMutableArray array];
    for (NSUInteger i =1; i<=12; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld",i]];
        [idleImages2 addObject:image];
    }
    // 设置普通状态的动画图片
    [headNew setImages:idleImages2 forState:MJRefreshStateIdle];
    // 设置即将刷新状态的动画图片（一松开就会刷新的状态）
    [headNew setImages:refreshingImages2 forState:MJRefreshStatePulling];
    // 设置正在刷新状态的动画图片
    [headNew setImages:refreshingImages2 duration:2 forState:MJRefreshStateRefreshing];
    // 隐藏状态
    headNew.stateLabel.hidden = YES;
    headNew.lastUpdatedTimeLabel.hidden = YES;
    self.tableView.mj_header = headNew;
    
    
    
    
    
    MJRefreshBackGifFooter *_header = [MJRefreshBackGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(requestDtaLoadMore)];
    NSMutableArray *refreshingImages = [NSMutableArray array];
    for (NSUInteger i =1; i<=12; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld",i]];
        [refreshingImages addObject:image];
    }
    
    NSMutableArray *idleImages = [NSMutableArray array];
    for (NSUInteger i =1; i<=12; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld",i]];
        [idleImages addObject:image];
    }
    // 设置普通状态的动画图片
    [_header setImages:idleImages forState:MJRefreshStateIdle];
    // 设置即将刷新状态的动画图片（一松开就会刷新的状态）
    [_header setImages:refreshingImages forState:MJRefreshStatePulling];
    // 设置正在刷新状态的动画图片
    [_header setImages:refreshingImages duration:2 forState:MJRefreshStateRefreshing];
    // 隐藏状态
    _header.stateLabel.hidden = YES;
    self.tableView.mj_footer = _header;

   
}
#pragma mark －＝－－＝－＝＝－＝＝－＝－－＝＝－collectionview的刷 新,加载事件
- (void) requestRefresh{
    _page = 1;
    [self getNoTimeBuyDataFromApi];
}
- (void)requestDtaLoadMore {
    _page++;
    [self getNoTimeBuyDataFromApi];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return _datarray.count +1;
    }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return 1;
    }

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        if (indexPath.section == 0) {
            return 150;
        }else {
            return 203;
        }
        
    }

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
        if (section == 0) {
            return 0;
        }else {
            return 10;
        }
    }

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)
section {
        UIView *bgV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 10)];
        bgV.backgroundColor = KShouYeBg;
        return bgV;
    }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        if (indexPath.section == 0) {
            NoTimeBuyOneCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NoTimeBuyOneCell"];
            cell.selectionStyle = kSeNone;
            return cell;
        }else {
            
            ShouYeSixCell *  cell = [_cellNib instantiateWithOwner:nil options:nil][0];
            
            cell.selectionStyle = kSeNone;
            NearlyShopModel *modle = self.datarray[indexPath.section -1];
            NSLog(@"--=-=-=%@",modle.shop_name);
            [cell.logoV sd_setImageWithURL:[NSURL URLWithString:modle.logo] placeholderImage:[UIImage imageNamed:@"quetu"]];
            cell.nameLB.text = modle.shop_name;
            
            if (modle.goods_data.count == 0) {
                
            }else if (modle.goods_data.count == 1) {
                NearlyShopModel2 *modl2 = modle.goods_data[0];
                cell.oneLB.text = modl2.name;
                [cell.oneImageV sd_setImageWithURL:[NSURL URLWithString:modl2.image]];
                if ([modl2.is_hot isEqualToString:@"1"]) {
                    cell.hotOneLB.hidden = NO;
                }else {
                    cell.hotOneLB.hidden = YES;
                }
            }
            
            else if (modle.goods_data.count == 2) {
                NearlyShopModel2 *modl2 = modle.goods_data[0];
                cell.oneLB.text = modl2.name;
                [cell.oneImageV sd_setImageWithURL:[NSURL URLWithString:modl2.image]];
                
                NearlyShopModel2 *modl3 = modle.goods_data[1];
                cell.twoLB.text = modl3.name;
                [cell.twoImageV sd_setImageWithURL:[NSURL URLWithString:modl3.image]];
                if ([modl2.is_hot isEqualToString:@"1"]) {
                    cell.hotOneLB.hidden = NO;
                }else {
                    cell.hotOneLB.hidden = YES;
                }
                if ([modl3.is_hot isEqualToString:@"1"]) {
                    cell.hotOneLB.hidden = NO;
                }else {
                    cell.hotOneLB.hidden = YES;
                }
            }
            
            else if (modle.goods_data.count == 3) {
                NearlyShopModel2 *modl2 = modle.goods_data[0];
                cell.oneLB.text = modl2.name;
                [cell.oneImageV sd_setImageWithURL:[NSURL URLWithString:modl2.image]];
                
                NearlyShopModel2 *modl3 = modle.goods_data[1];
                cell.twoLB.text = modl3.name;
                [cell.twoImageV sd_setImageWithURL:[NSURL URLWithString:modl3.image]];
                
                
                NearlyShopModel2 *modl4 = modle.goods_data[2];
                cell.thirdLB.text = modl4.name;
                [cell.threeImage sd_setImageWithURL:[NSURL URLWithString:modl4.image]];
               
                if ([modl2.is_hot isEqualToString:@"1"]) {
                    cell.hotOneLB.hidden = NO;
                }else {
                    cell.hotOneLB.hidden = YES;
                }
                if ([modl3.is_hot isEqualToString:@"1"]) {
                    cell.hotOneLB.hidden = NO;
                }else {
                    cell.hotOneLB.hidden = YES;
                }
                if ([modl4.is_hot isEqualToString:@"1"]) {
                    cell.hotOneLB.hidden = NO;
                }else {
                    cell.hotOneLB.hidden = YES;
                }
            }
            
            else if (modle.goods_data.count == 4) {
                NearlyShopModel2 *modl2 = modle.goods_data[0];
                cell.oneLB.text = modl2.name;
                [cell.oneImageV sd_setImageWithURL:[NSURL URLWithString:modl2.image]];
                
                NearlyShopModel2 *modl3 = modle.goods_data[1];
                cell.twoLB.text = modl3.name;
                [cell.twoImageV sd_setImageWithURL:[NSURL URLWithString:modl3.image]];
                
                
                NearlyShopModel2 *modl4 = modle.goods_data[2];
                cell.thirdLB.text = modl4.name;
                [cell.threeImage sd_setImageWithURL:[NSURL URLWithString:modl4.image]];
                
                
                NearlyShopModel2 *modl5 = modle.goods_data[3];
                cell.fourLB.text = modl5.name;
                [cell.fourimageV sd_setImageWithURL:[NSURL URLWithString:modl5.image]];
                
                if ([modl2.is_hot isEqualToString:@"1"]) {
                    cell.hotOneLB.hidden = NO;
                }else {
                    cell.hotOneLB.hidden = YES;
                }
                if ([modl3.is_hot isEqualToString:@"1"]) {
                    cell.hotOneLB.hidden = NO;
                }else {
                    cell.hotOneLB.hidden = YES;
                }
                if ([modl4.is_hot isEqualToString:@"1"]) {
                    cell.hotOneLB.hidden = NO;
                }else {
                    cell.hotOneLB.hidden = YES;
                }
                if ([modl5.is_hot isEqualToString:@"1"]) {
                    cell.hotOneLB.hidden = NO;
                }else {
                    cell.hotOneLB.hidden = YES;
                }
            }
            
            
            return cell;
            
        }
    }
    

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        if (indexPath.section == 0) {
            return;
        }
        else {
           NearlyShopModel *modle = self.datarray[indexPath.section -1];;
            
            InStoreController *store = [[InStoreController alloc] init];
            //store.hidesBottomBarWhenPushed = YES;
            store.shopID = modle.m_id;
            [self.navigationController pushViewController:store animated:YES];
        }
        
    }
    
    
#pragma mark －＝－＝－＝－＝－＝－＝－＝－＝获取限时秒杀数据＝＝＝＝

- (void)getNoTimeBuyDataFromApi {
        NSDictionary *saveDic = [SaveDataMethord readLoginData];
        NSDictionary *parameDic = @{@"userid":saveDic[@"userid"],@"page":[NSString stringWithFormat:@"%d",_page],@"promotion_id":_manJianId,@"limit":@"10",@"actoken":[Md5Apiway getMD5data:saveDic[@"userid"]]};
        
        
        [HttpRequest getWithURLString:POST(Shop_PromotionGetPromotionShop) parameters:parameDic success:^(id responseObject) {
            
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            if (_page ==1) {
                [_datarray removeAllObjects];
            }

            if ([responseObject[@"ret"] intValue] != 200) {
                [MBProgressHUD showText:@"获取限时秒杀商品出错"];
                return ;
            }
            NSDictionary *tempD = responseObject[@"data"];
            
            if (![[NSString stringWithFormat:@"%@",tempD[@"code"]] isEqualToString:@"0"]) {
                [MBProgressHUD showText:tempD[@"msg"]];
                return;
            }
            
            _datarray33 =[NearlyShopModel mj_objectArrayWithKeyValuesArray:[[responseObject objectForKey:@"data"] objectForKey:@"shop_data"]];
            
            [_datarray addObjectsFromArray:_datarray33.mutableCopy];
            
            
            
            [self.tableView reloadData];
        } failure:^(NSError *error) {

        }];
        
    }
    
    
    


    
    
@end

